<?php 

Class menuController extends appController
{
	
	public function index()
	{
		
		$this->admin_priv('menuindex');
	   	$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
	   	$sort['keyword']=isset($_GET['k'])?trim($_GET['k']):'';
	   	$menu_list=Model('menu')->get_menu_list2($page,10,$sort);
		$this->assign('ur_here','>菜单管理');
		$this->assign('search_action','1');
		$this->assign('menu_list',$menu_list['data']);
		$this->assign('menu_page',$menu_list['pagelink']);
		
		
		$this->display('menu_list.html');
	
	}

	
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('menu')->ordering($val,$id);
		print_r(json_encode($result));
	}
	
	public function add_menu()
	{
		$this->admin_priv('addmenu');
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		
		$this->assign('ur_here','>添加栏目');
		$this->assign('action','create_menu');
		$this->display('add_menu.html');
	
	}
	
	public function create_menu()
	{
		$this->admin_priv('addmenu');
		$info['parent_id']=isset($_POST['parent_id'])?intval($_POST['parent_id']):0;
		$info['name']=isset($_POST['name'])?$_POST['name']:'';
		$en_name=isset($_POST['en_name'])?$_POST['en_name']:'';
		$info['name']=CH_SC.$info['name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['is_show']=isset($_POST['is_show'])?$_POST['is_show']:0;
		$info['code']=isset($_POST['code'])?$_POST['code']:'';
		$info['is_extend_links']=isset($_POST['is_extend_links'])?intval($_POST['is_extend_links']):0;
		$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
		$data=array('error'=>0,'content'=>'','url'=>'');
		
		if($_POST['name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('title_empty');
			$this->showMsg($data);
		}
		
		if (hy_check($info['code'],'un')=='#@_error')
		{
			$data['error']=2;
			$data['content']=Lang('code_error');
			$this->showMsg($data);
		}
	
		if($info['is_extend_links']==1)
		{
		 $info['redirect_url']=isset($_POST['redirect_url'])?$_POST['redirect_url']:'';
		 if (hy_check( $info['redirect_url'],'u')=='#@_error'&&$info['redirect_url']!='http://')
		 {
		 	$data['error']=3;
		 	$data['content']=Lang('url_error');
		 	$this->showMsg($data);
		 }
		 
		}
		else 
		{
			
		
		$info['type']=isset($_POST['type'])?intval($_POST['type']):'';
		$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['description']=isset($_POST['description'])?$_POST['description']:'';
		}
		
		if(Model('menu')->check_code($info['code']))
		{
		   $result=Model('menu')->insert_menu($info);
		  
		     if($result)
		   {
			$data['content']=Lang('SUCCESS_ADD');
			$data['url']="index.php?g=admin&m=menu&a=index";
			
		   }
		   else
		  {
		  	$data['error']=4;
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		   }
		   $this->showMsg($data);
		}
		else 
		{
			$data['error']=1;
			$data['content']=Lang('CODE_REPEATING');
			$this->showMsg($data);
		}
		
	} 
	public function del()
	{
		$this->admin_priv('delmenu',2);
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
			 //hungry:栏目没有批量删除 $result=Model('menu')->del_menu($v);
			}
		}
		else 
		{
			$result=Model('menu')->del_menu($id);
		}
		print_r(json_encode($result));
	}
	
	public function file()
	{
	
		$file=current($_FILES);
		$result=array('error'=>0,'url'=>'');
		$thumb_url=Model('image')->upload_image($file, 'menu');
		$result['url']=$thumb_url;
	
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
	public function edit_menu()
	{
		$this->admin_priv('editmenu');
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$menu_info=Model('menu')->get_menu_info($id);
		$this->assign('parent_list',Model('menu')->get_parent_menu());
		$this->assign('menu',$menu_info);
		$this->assign('id',$id);
		$this->assign('ur_here','>编辑栏目');
		$this->assign('action','update_menu');
		$this->display('add_menu.html');
	
	}
	public function update_menu()
	{
		$this->admin_priv('editmenu');
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['parent_id']=isset($_POST['parent_id'])?intval($_POST['parent_id']):0;
		$info['name']=isset($_POST['name'])?$_POST['name']:'';
		$en_name=isset($_POST['en_name'])?$_POST['en_name']:'';
		$info['name']=CH_SC.$info['name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['is_show']=isset($_POST['is_show'])?intval($_POST['is_show']):0;
		$info['code']=isset($_POST['code'])?$_POST['code']:'';
		$data=array('error'=>0,'content'=>'','url'=>'');
		$info['is_extend_links']=isset($_POST['is_extend_links'])?intval($_POST['is_extend_links']):0;
		
		if($info['parent_id']==$id)
		{
			$data['content']=Lang('PARENT_ID_ERR');
			$this->showMsg($data);
		}
		
		if($_POST['name']=='')
		{
			$data['error']=1;
			$data['content']=Lang('title_empty');
			$this->showMsg($data);
		}
		
		if (hy_check($info['code'],'un')=='#@_error')
		{
			$data['error']=2;
			$data['content']=Lang('code_error');
			$this->showMsg($data);
		}
		
		if($info['is_extend_links']==1)
		{
			$info['redirect_url']=isset($_POST['redirect_url'])?$_POST['redirect_url']:'';
			if (hy_check( $info['redirect_url'],'u')=='#@_error'&&$info['redirect_url']!='http://')
			{
				$data['error']=3;
				$data['content']=Lang('url_error');
				$this->showMsg($data);
			}
			$info['type']='';
			$info['page_title']='';
			$info['keywords']='';
			$info['description']='';
		}
		else
		{
			
			$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
			$info['type']=isset($_POST['type'])?intval($_POST['type']):'';
			$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
			$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
			$info['description']=isset($_POST['description'])?$_POST['description']:'';
			$info['redirect_url']='';
		}
		
		if(Model('menu')->check_code($info['code'],$id))
		{
			$result=Model('menu')->update_menu($info,$id);
		
			if($result)
			{
				$data['content']=Lang('SUCCESS_EDIT');
				$data['url']="index.php?g=admin&m=menu&a=index";
					
			}
			else
			{
				$data['content']=Lang('INVALID_OPERATION');
				//$data['url']=$_SERVER['REQUEST_URI'];
			}
			$this->showMsg($data);
		}
		else
		{
			$data['content']=Lang('CODE_REPEATING');
			$this->showMsg($data);
		}
		
	}
	
	
}